package com.webSite.demo.cy.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.webSite.demo.cy.bean.HomeContactUs;
import com.webSite.demo.cy.mapper.HomeContactUsMapper;

@Controller
@EnableAutoConfiguration
public class HomeContactUsController {

	@Autowired
	HomeContactUsMapper contactUsMapper;
	
	@RequestMapping("/contactList.html")  
    String contactList(Model model) {
		List<HomeContactUs> list= contactUsMapper.findAllInfo();
		if(!list.isEmpty()) {
			model.addAttribute("list", list);
		}
        return "admin/contactList";  
    }
	
	@RequestMapping("/addHomeContactUs.html")
	public String addHomeContactUs(HomeContactUs contact) {
		contact.setCreated(new Date());
		contact.setUpdated(new Date());
		contactUsMapper.insertContactUs(contact);
		return "redirect:/homeContactUs.html";
	}
	
	@RequestMapping("/updateContactState.html")
	@ResponseBody
	public String updateState(int[] ids) {
		for(int id:ids) {
			contactUsMapper.updateState(id);
		}
		return "ok";
	}
	
	//没有特殊需求采用虚拟删除
	@RequestMapping("/deleteHomeContactUs.html")
	@ResponseBody
	public String deleteHomeContactUs(int id) {
		contactUsMapper.updateState(id);
		return "ok";
	}
	
	//修改页面跳转
	@RequestMapping("/updateContactUs.html")
	public String updateContactUs(int id,Model model) {
		HomeContactUs contactUs = contactUsMapper.selectOne(id);
		model.addAttribute("contactUs", contactUs);
		return "admin/update/updateContactUs";
	}
	
	@RequestMapping("/updateHomeContactUs.html")
	public String updateHomeContactUs(HomeContactUs homeContactUs) {
		homeContactUs.setUpdated(new Date());
		contactUsMapper.updateContactUs(homeContactUs);
		return "redirect:/contactList.html";
	}
}
